package it.uniroma2.art.owlart.langsearch;

import it.uniroma2.art.owlart.langsearch.index.RDFIndexManager;
import it.uniroma2.art.owlart.langsearch.index.config.StandardAnalysisConfiguration;
import it.uniroma2.art.owlart.langsearch.search.OntIndexSearcher;
import it.uniroma2.art.owlart.model.ARTURIResource;
import it.uniroma2.art.owlart.protegeimpl.factory.ARTModelFactoryProtegeImpl;
import it.uniroma2.art.owlart.protegeimpl.models.OWLModelProtegeImpl;
import it.uniroma2.art.owlart.protegeimpl.start.AGROVOC_INFO;

import java.util.ArrayList;
import java.util.Iterator;

import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.stanford.smi.protegex.owl.model.OWLIndividual;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;

public class AGROVOC_CUSTOMSEARCH_ProtegeDB_TestOld {

	protected static Logger logger = LoggerFactory.getLogger(AGROVOC_CUSTOMSEARCH_ProtegeDB_TestOld.class);

	protected static String agrovocBaseNS = "http://aims.fao.org/aos/v1.0/";

	protected static String dbusername = "root";
	protected static String dbpwd = "mysqlstarred";
	
	public static void main(String[] args) {
		try {

			String chosenDB = AGROVOC_INFO.current_agrovoc_small_DB;
			
			ARTModelFactoryProtegeImpl fact = new ARTModelFactoryProtegeImpl();			
			
			OWLModelProtegeImpl model = (OWLModelProtegeImpl) fact.loadOWLModel("com.mysql.jdbc.Driver",
					"jdbc:mysql://127.0.0.1:3306/" + chosenDB
							+ "?requireSSL=false&useUnicode=true&characterEncoding=UTF-8", chosenDB,
					dbusername, dbpwd);

			String indexLocation = "./data/index";

			StandardAnalysisConfiguration conf = new StandardAnalysisConfiguration(Version.LUCENE_36, "en", "it");
			// conf.setLanguagesOfURILocalNames("en", "it");
			OWLSearchEngine oi = new OWLSearchEngine(model, indexLocation, conf);
			
			String customIndexName = "c_nounInstances";
			
			ArrayList<String> indexes = new ArrayList<String>();
			indexes.add(customIndexName);
			oi.initializeCustomIndexWriters(indexes);

			// INDEX BUILDING (decomment this to redo indexing)
			// System.out.println("building index at: " + indexLocation);
			// ARTURIResource cnoun = model.createURIResource(agrovocBaseNS + "c_noun");
			// ARTURIResourceIterator it = new
			// URIResourceIteratorFilteringResourceIterator(model.listInstances(
			// cnoun, false));
			// oi.buildCustomIndex(customIndexName, it, true);
			// logger.info("INDEX BUILT");

			oi.createOntIndexSearcher();
			System.out.println("ontsearcher armed");

			Iterator<ARTURIResource> retrievedResources;
			Iterator<String> retrievedLabels;

			// *************************************
			// example over AGROVOC ontology
			// *************************************

			oi.setTHRESHOLD(0.7);

			retrievedLabels = oi.findLabelsForIndex(customIndexName, "deer", "en",
					OntIndexSearcher.FUZZY_MATCH).iterator();
			while (retrievedLabels.hasNext())
				System.out.println(retrievedLabels.next());

			retrievedLabels = oi.findLabelsForIndex(customIndexName, "cervidi", "it",
					OntIndexSearcher.FUZZY_MATCH).iterator();
			while (retrievedLabels.hasNext())
				System.out.println(retrievedLabels.next());

			retrievedLabels = oi.findLabelsForIndex(customIndexName, "\"Concetti dominio\"", "it",
					OntIndexSearcher.FUZZY_MATCH).iterator();
			while (retrievedLabels.hasNext())
				System.out.println(retrievedLabels.next());

			retrievedResources = oi.findResourcesForIndex(customIndexName, "deer", "en",
					OntIndexSearcher.FUZZY_MATCH).iterator();
			while (retrievedResources.hasNext())
				System.out.println(retrievedResources.next());
			
			
			
			// TEST FOR CREATING NEW TERM AND ADDING IT TO THE INDEX
			
			System.err.println("\n\n\nNOW TESTING ADDITION OF CONCEPT TO INDEX\n\n\n");
			
			RDFIndexManager idxMgr = oi.getIndexManager();
			
			String newTermString = "newTerm";
			
			OWLModel protOWLModel = model.getProtegeOWLModel();
			OWLNamedClass nounCls = protOWLModel.getOWLNamedClass("c_noun");
			OWLIndividual newTermProt = (OWLIndividual)protOWLModel.getInstance(newTermString); 
			if (newTermProt==null) {
				newTermProt = (OWLIndividual) nounCls.createInstance(newTermString);
			}			
			newTermProt.addLabel("nuovo termine", "it");
			
			System.out.println("newTermProt URI: " + newTermProt.getURI());
			
			ARTURIResource newTerm = model.createURIResource(newTermProt.getURI());
			//model.addLabel(newTerm, "nuovo termine", "it");
			
			idxMgr.addNewResourceToIndex(newTerm, customIndexName);
			idxMgr.updateLangIndexForResource(newTerm, customIndexName, "it");
			
			retrievedLabels = oi.findLabelsForIndex(customIndexName, "termine", "it",
					OntIndexSearcher.FUZZY_MATCH).iterator();
			while (retrievedLabels.hasNext())
				System.out.println(retrievedLabels.next());

			retrievedResources = oi.findResourcesForIndex(customIndexName, "termine", "it",
					OntIndexSearcher.FUZZY_MATCH).iterator();
			while (retrievedResources.hasNext())
				System.out.println(retrievedResources.next());
			
			idxMgr.deleteResourceFromIndexes(newTerm, customIndexName);
			
			retrievedLabels = oi.findLabelsForIndex(customIndexName, "termine", "it",
					OntIndexSearcher.FUZZY_MATCH).iterator();
			while (retrievedLabels.hasNext())
				System.out.println(retrievedLabels.next());

		} catch (Exception e) {
			e.printStackTrace(System.err);
			logger.error(e.getMessage());

			System.err.flush();
			System.err.println("error");

			e.printStackTrace();
		}
	}

}
